<?php
namespace Common\Controller;
use Common\Controller\HomebaseController;

class AppserviceController extends HomebaseController {
	
	function _initialize() {
		parent::_initialize();
	}
	function checkMember($vars){
		$record = array ('status' => 1,'msg' => '不需要进行会员登录校验！');
		$mid = $vars ['member_id'];
		$token = $vars ['token'];
		if(!empty($mid)){
			$record = array ('status' => 0,'code'=>'10000','msg' => '您还没有登录，请先登录！');
			$userinfo = $this->autoLogin ($mid,$token);
			if($userinfo['status']!='1'){
				return $userinfo;
			}
			$record = array ('status' => 1,'msg' => '重新登录成功！','data' => $userinfo);
		}else{
			$record = array ('status' => '10000','msg' => '您还没有登录，请重新登录！');
		}
		return $record;
	}
	public function  autoLogin($mid,$token){
		$member = M('Member')->where(array("id"=>$mid))->find();
		if(!empty($member) && is_array($member)){
			if($member['token'] == $token){
				if($member['status']==1){
					$record = $member;
				}else{
					$record = array ('status' => '10000','msg' => '此账号已经被禁止使用，请联系管理员！');
				}
			}else{
				$record = array ('status' => '10002','msg' => '您还没有登录或您的账户已在另一台手机登录！');
			}
		}else{
				$record = array ('status' => '10002','msg' => '您还没有登录或您的账户已在另一台手机登录！');
		}
		return $record;
	}
	/**
	 * 获取时间 年月日前
	 */
	public function getTimeBefor($create_time){
		$time['i'] = date("i",time()-strtotime($create_time)) - date("i",0);
		$time['h'] = date("G",time()-strtotime($create_time)) - date("G",0);
		$time['d'] = date("j",time()-strtotime($create_time)) - date("j",0);
		$time['m'] = date("n",time()-strtotime($create_time)) - date("n",0);
		$time['y'] = date("Y",time()-strtotime($create_time)) - date("Y",0);
		$_record = !empty($time['y'])?$time['y']."年前":(!empty($time['m'])?$time['m']."月前":(!empty($time['d'])?$time['d']."天前":(!empty($time['h'])?$time['h']."小时前":(!empty($time['i'])?$time['i']."分钟前":"刚刚创建"))));
		return $_record;
	}
	public function getAddress($lat,$log){
		$content = file_get_contents("http://api.map.baidu.com/geocoder?location=".$lat.",".$log."&output=xml&key=28bcdd84fae25699606ffad27f8da77b");
		$json = $this->xmlToArray($content);
		$arr['status'] = $json['status'];
		//$arr['province'] = $json['result']['addressComponent']['province'];
		$arr['city'] = $json['result']['addressComponent']['city'];
		//$arr['area'] = $json['result']['addressComponent']['district'];
		return $arr;
	}
	public function xmlToArray($xml)
	{
		//将XML转为array
		$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $array_data;
	}
	
	public function getOrderSn(){ // 获取订单号
		$yCode = array(
    			'Y',
    			'O',
    			'N',
    			'G',
    			'Z',
    			'H',
    			'E'
    	);
		$order_sn = $yCode[intval(date('w'))] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), - 5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
		return $order_sn;
	}
	
	public function getSwim(){ // 获取泳者号
		$yCode = array(
				'Y',
				'O',
				'N',
				'G',
				'Z',
				'H',
				'E'
		);
		$sn = strtoupper(dechex(date('m'))) . date('d') . substr(time(), - 5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
		$swim_sn = $yCode[intval(date('w'))] . substr($sn, 0,8);
		return $swim_sn;
	}
	
	public function getToken(){ // 注册登录设备号
		$sn = strtoupper(dechex(date('m'))) . date('d') . substr(time(), - 5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
		$swim_sn = substr($sn, 0,8);
		return $swim_sn;
	}
	
	public function cityarea() {
		$_file = RUNTIME_PATH . "Data/cityarea.php";
		if (file_exists($_file)){
			$_cityarea = require $_file;
			return $_cityarea;
		}else{
			return $this->docityarea();
		}
	}
	public function docityarea(){
		$this->TmpStrs = '';
		$this->writeCityarea();
		$TmpStr = "<?php\r\n";
		$TmpStr .= "return array(\r\n";
		$TmpStr .= $this->TmpStrs;
		$TmpStr .= ");\r\n";
		$TmpStr .= "?>";
		if (! file_exists(RUNTIME_PATH . "Data/")) {
			mkdir(RUNTIME_PATH . "Data/");
		}
		$this->Filename = $filePath = RUNTIME_PATH . "Data/". "/cityarea.php";
		$fp = fopen($this->Filename, "w+");
		fwrite($fp, $TmpStr);
		fclose($fp);
		$_cityarea = require $filePath;
		return $_cityarea;
	}
	public function writeCityarea(){
		$Arr = $this->makeCityarea();
		$i = 0;
		if (! empty($Arr) && is_array($Arr)) {
			foreach ($Arr as $key => $val) {
				$this->TmpStrs .="\t'{$val['region_id']}'=>array(\r\n";
				$this->TmpStrs .="\t\t'region_id'=>" . "'{$val['region_id']}', \r\n";
				$this->TmpStrs .="\t\t'parent_id'=>" . "'{$val['parent_id']}', \r\n";
				$this->TmpStrs .="\t\t'region_name'=>" . "'{$val['region_name']}', \r\n";
				$this->TmpStrs .="\t\t'region_type'=>" . "'{$val['region_type']}', \r\n";
				$this->TmpStrs .="\t\t'modules_sub'=>array(\r\n";
				foreach ($val['modules_sub'] as $key_key=>$val_val){
					$this->TmpStrs .="\t\t\t'{$val_val['region_id']}'=>array(\r\n";
					$this->TmpStrs .="\t\t\t\t'region_id'=>" . "'{$val_val['region_id']}', \r\n";
					$this->TmpStrs .="\t\t\t\t'parent_id'=>" . "'{$val_val['parent_id']}', \r\n";
					$this->TmpStrs .="\t\t\t\t'region_name'=>" . "'{$val_val['region_name']}', \r\n";
					$this->TmpStrs .="\t\t\t\t'region_type'=>" . "'{$val_val['region_type']}', \r\n";
					$this->TmpStrs .="\t\t\t\t'modules_sub'=>array(\r\n";
					foreach ($val_val['modules_sub'] as $key_key_key=>$val_val_val){
						$this->TmpStrs .="\t\t\t\t\t'{$val_val_val['region_id']}'=>array(\r\n";
						$this->TmpStrs .="\t\t\t\t\t\t'region_id'=>" . "'{$val_val_val['region_id']}', \r\n";
						$this->TmpStrs .="\t\t\t\t\t\t'parent_id'=>" . "'{$val_val_val['parent_id']}', \r\n";
						$this->TmpStrs .="\t\t\t\t\t\t'region_name'=>" . "'{$val_val_val['region_name']}', \r\n";
						$this->TmpStrs .="\t\t\t\t\t\t'region_type'=>" . "'{$val_val_val['region_type']}', \r\n";
						$this->TmpStrs .= "\t\t\t\t\t),\r\n";
					}
					$this->TmpStrs .= "\t\t\t\t),\r\n";
					$this->TmpStrs .= "\t\t\t),\r\n";
				}
				$this->TmpStrs .= "\t\t),\r\n";
				$this->TmpStrs .= "\t),\r\n";
			}
		}
	}
	public function makeCityarea(){
		$City=M("region")->where("parent_id='1' and region_type='1'")->field("region_id,parent_id,region_name,region_type")->select();
		if(!empty($City) && is_array($City)){
			foreach ($City as $key=>$val){
				$City[$key]['modules_sub'] = M("region")->where("parent_id='{$val['region_id']}' and region_type='2'")->field("region_id,parent_id,region_name,region_type")->select();
				if(!empty($City[$key]['modules_sub']) && is_array($City[$key]['modules_sub'])){
					foreach ($City[$key]['modules_sub'] as $k=>$v){
						$City[$key]['modules_sub'][$k]['modules_sub'] = M("region")->where("parent_id='{$v['region_id']}' and region_type='3'")->field("region_id,parent_id,region_name,region_type")->select();
					}
				}
			}
		}
		return $City;
	}
}